package org.xlab.dictionary.dao.postgresql;

import java.sql.Types;
import java.util.Set;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.xlab.dictionary.dao.AbstractDao;
import org.xlab.dictionary.dao.DaoException;
import org.xlab.dictionary.dao.ITypeDao;
import org.xlab.dictionary.domain.Type;

@Repository("typeDao")
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class TypeDao extends AbstractDao implements ITypeDao {

	public static class Query {

		protected Query() {
		}

		public static final String CREATE_STATEMENT = "INSERT INTO types (type_id, type_name, title, not_instantiable, not_inheritable, parent_type_id) VALUES (:typeId, :typeName, :title, :notInstantiable, :notInheritable, :parentTypeId)";

		public static final String READ_STATEMENT = "SELECT type_id, type_name, title, not_instantiable, not_inheritable FROM types WHERE type_id = :typeId";

		public static final String UPDATE_STATEMENT = "";

		public static final String DELETE_STATEMENT = "";

		public static final String READ_BY_NAME_STATEMENT = "SELECT type_id, type_name, title, not_instantiable, not_inheritable FROM types WHERE type_name = :typeName";

		public static final String DELETE_BY_NAME_STATEMENT = "";

		public static final String GET_SUPERTYPE_BY_NAME_STATEMENT = "";

		public static final String GET_SUPERTYPE_STATEMENT = "";

		public static final String GET_SUBTYPES_BY_NAME_STATEMENT = "";

		public static final String GET_SUBTYPES_STATEMENT = "";

		public static final String INSTANCE_OF_STATEMENT = "";

		public static final String INSTANCE_OF_BY_NAME_STATEMENT = "";

	}

	public void deleteByName(String name) throws DaoException {
		// TODO Auto-generated method stub
	}

	public Set<Type> getSubtypesByName(String name) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public Set<Type> getSubtypes(Integer id) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public Type getSupertypeByName(String name) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public Type getSupertype(Integer id) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean instanceOf(Integer id, Integer supertypeId) throws DaoException {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean instanceOf(String name, String supertypeName) throws DaoException {
		// TODO Auto-generated method stub
		return false;
	}

	public Type readByName(String name) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public Type create(Type entity) throws DaoException {
		MapSqlParameterSource parameterSource = new MapSqlParameterSource();
		parameterSource.addValue("typeId", entity.getId(), Types.INTEGER);
		parameterSource.addValue("typeName", entity.getName(), Types.VARCHAR);
		parameterSource.addValue("title", entity.getTitle(), Types.VARCHAR);
		parameterSource.addValue("notInstantiable", entity.isNotInstantiable() ? 1 : 0, Types.SMALLINT);
		parameterSource.addValue("notInheritable", entity.isNotInheritable() ? 1 : 0, Types.SMALLINT);
		parameterSource.addValue("parentTypeId", null, Types.INTEGER);

		KeyHolder keyHolder = new GeneratedKeyHolder();

		getJdbcTemplate().update(Query.CREATE_STATEMENT, parameterSource, keyHolder, new String[] { "type_id" });
		int entityId = keyHolder.getKey().intValue();
		entity.setId(entityId);

		return entity;
	}

	public void delete(Integer id) throws DaoException {
		// TODO Auto-generated method stub

	}

	public Type read(Integer id) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public Type update(Type entity) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public Type create(Type entity, Integer supertypeId) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

	public Type create(Type entity, String supertypeName) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}

}
